package cn.house.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.house.entity.District;
import cn.house.entity.House;
import cn.house.entity.Street;
import cn.house.entity.User;
import cn.house.service.DistrictService;
import cn.house.service.DistrictServiceImpl;
import cn.house.service.HouseService;
import cn.house.service.HouseServiceImpl;
import cn.house.service.StreetService;
import cn.house.service.StreetServiceImpl;

public class ManageAction extends ActionSupport {

	private HouseService houseService = new HouseServiceImpl();
	private DistrictService districtService = new DistrictServiceImpl();
	private StreetService streetService = new StreetServiceImpl();
	private List<House> result = new ArrayList<House>();
	private List<District> districtList;

	/**
	 * 查询当前登录用户发布房屋列表
	 * 
	 * @return
	 */
	public String list() {
		User user = (User) ActionContext.getContext().getSession().get("userSession");
		result = houseService.selectHouseByUserId(user.getId());
		return Action.SUCCESS;
	}

	private String title;

	public String searchList() {
		System.out.println("searchList");
		// 查询所有
		result = houseService.selectAllHouse(title);
		return "search_list";
	}

	public String toadd() {
		// 查询房屋类型
		// 查询区县
		districtList = districtService.listAll();
		return "toadd";
	}

	private Integer district_id;

	public void loadStreet() {
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		List<Street> streetList = streetService.listStreetByDid(district_id);
		List<Street> tempList = new ArrayList<>();
		for (Street street : streetList) {
			tempList.add(new Street(street.getId(), street.getName()));
		}
		String jsonStr = JSON.toJSONString(tempList);
		try {
			ServletActionContext.getResponse().getWriter().print(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private House house;

	public String add() {
		User user = (User) ActionContext.getContext().getSession().get("userSession");
		house.setUser(user);

		// 根据街道id查询街道
		house.getStreet().getId();
		Street street = null;

		house.setStreet(street);
		return "add_sucess";
	}

	public Integer getDistrict_id() {
		return district_id;
	}

	public void setDistrict_id(Integer district_id) {
		this.district_id = district_id;
	}

	public HouseService getHouseService() {
		return houseService;
	}

	public void setHouseService(HouseService houseService) {
		this.houseService = houseService;
	}

	public List<House> getResult() {
		return result;
	}

	public void setResult(List<House> result) {
		this.result = result;
	}

	public DistrictService getDistrictService() {
		return districtService;
	}

	public void setDistrictService(DistrictService districtService) {
		this.districtService = districtService;
	}

	public List<District> getDistrictList() {
		return districtList;
	}

	public void setDistrictList(List<District> districtList) {
		this.districtList = districtList;
	}

	public StreetService getStreetService() {
		return streetService;
	}

	public void setStreetService(StreetService streetService) {
		this.streetService = streetService;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public House getHouse() {
		return house;
	}

	public void setHouse(House house) {
		this.house = house;
	}

}
